package com.szh.cloud.role.control;

import com.szh.cloud.mybatis.entity.PtRoleEntity;
import com.szh.cloud.mybatis.flexigrid.FlexResEntity;
import com.szh.cloud.mybatis.flexigrid.PagingBean;
import com.szh.cloud.mybatis.ztree.ZtreeEntity;
import com.szh.cloud.role.entity.SearchRoleEntity;
import com.szh.cloud.role.exception.RoleNameCcupdException;
import com.szh.cloud.role.service.RoleService;
import com.szh.cloud.spring.control.BaseController;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;
    @Secured("ROLE_cldk_data_role")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @RequestMapping("/queryRoleListByPage")
    @ResponseBody
    public FlexResEntity<PtRoleEntity> queryRoleListByPage(SearchRoleEntity entity, PagingBean paging){

        FlexResEntity<PtRoleEntity> flexResEntity = roleService.queryRoleListByPage(entity,paging);
        return flexResEntity;
    }
    @RequestMapping("/queryMenuByRoleId")
    @ResponseBody
    public List<ZtreeEntity> queryMenuByRoleId(@RequestParam("roleUuid") String roleUuid){
        List<ZtreeEntity> list = roleService.queryMenuByRoleId(roleUuid);
        return list;
    }
    @RequestMapping("/saveMenuRefRole")
    @ResponseBody
    public String saveMenuRefRole(@RequestParam("roleUuids") String roleUuids,@RequestParam("menuIds") String menuIds){
        roleService.saveMenuRefRole(roleUuids,menuIds);
        return successJSON("role.menu.allocate.success");
    }
    @RequestMapping("/addRoleInfo")
    @ResponseBody
    public String addRoleInfo(PtRoleEntity entity){
        roleService.addRoleInfo(entity);
        return successJSON("role.add.success");
    }

    @RequestMapping("/modifyRoleInfo")
    @ResponseBody
    public String modifyRoleInfo(PtRoleEntity entity){
        roleService.modifyRoleInfo(entity);
        return successJSON("role.modify.success");
    }

    @RequestMapping("/deleteRoleById")
    @ResponseBody
    public String deleteRoleById(@RequestParam("roleUuids") String roleUuids){
        try {
            roleService.deleteRoleById(roleUuids);
        } catch (RoleNameCcupdException e) {
//            e.printStackTrace();
            return failJSON("delete.role.occupy");
        }
        return successJSON("delete.role.success");
    }
}
